// SplitUtils.cpp

#include "StdAfx.h"

#include "Common/String/StringToInt.h"

#include "SplitUtils.h"
#include "Common/String/StringUtils.h"

namespace Common
{
namespace FileSystem
{
	bool ParseVolumeSizes(const CString &s, std::vector<UINT64> &values)
{
  values.clear();
  CStringVector destStrings;
  String::SplitStringBySpace(s, destStrings);
  bool prevIsNumber = false;
  for (size_t i = 0; i < destStrings.size(); i++)
  {
    CString subString = destStrings[i];
    subString.MakeUpper();
    if (subString.IsEmpty())
      return false;
    if (subString == _T("-"))
      return true;
    if (prevIsNumber)
    {
      wchar_t c = subString[0];
      UINT64 &value = values.back();
      prevIsNumber = false;
      switch(c)
      {
        case _T('B'):
          continue;
        case _T('K'):
          value <<= 10;
          continue;
        case _T('M'):
          value <<= 20;
          continue;
        case _T('G'):
          value <<= 30;
          continue;
      }
    }
    const TCHAR *start = subString;
    const TCHAR *end;
    UINT64 value = String::ConvertStringToUInt64(start, &end);
    if (start == end)
      return false;
    if (value == 0)
      return false;
	values.push_back(value);
    prevIsNumber = true;
    CString rem = subString.Mid((int)(end - start));
    if (!rem.IsEmpty())
		destStrings.insert(destStrings.begin() + i + 1, rem);
  }
  return true;
}

void AddVolumeItems(CComboBox &volumeCombo)
{
  volumeCombo.AddString(TEXT("10M"));
  volumeCombo.AddString(TEXT("650M - CD"));
  volumeCombo.AddString(TEXT("700M - CD"));
  volumeCombo.AddString(TEXT("4480M - DVD"));
  volumeCombo.AddString(TEXT("1457664 - 3.5\" floppy"));
}

UINT64 GetNumberOfVolumes(UINT64 size, std::vector<UINT64> &volSizes)
{
  if (size == 0 || volSizes.size() == 0)
    return 1;
  UINT64 numVolumes = 0;
  for (std::vector<UINT64>::size_type i = 0; i < volSizes.size(); i++)
  {
    UINT64 volSize = volSizes[i];
    numVolumes++;
    if (volSize >= size)
      return numVolumes;
    size -= volSize;
  }
  UINT64 volSize = volSizes.back();
  if (volSize == 0)
    return (UINT64)(INT64)-1;
  return numVolumes + (size - 1) / volSize + 1;
}

}
}